public class A_Test {

	public static eBayAPI ebay = new eBayAPI();
	
	public static void doGetUser() {

		ebay.setupSecurity('GetUser');
		 
		GetUserRequestType.GetUserRequestType_e gur = new GetUserRequestType.GetUserRequestType_e();
		gur.setVersion('561');
		gur.setDetailLevel(new String[] {'ReturnAll'});
		gur.setUserID('testuser_djcarroll');
 
		GetUserResponseType.GetUserResponseType_e guResponse = ebay.GetUser(gur);

		System.debug(guResponse);
		if (guResponse.Ack == 'Failure') {
			System.debug(guResponse.Errors[0].LongMessage);
		} else {
			System.debug('Email address: ' + guResponse.User_x.Email);
			//System.debug('Reg Address: ' + guResponse.User_x.RegistrationAddress.Name);
			System.debug('Reg Address: ' + guResponse.User_x.RegistrationAddress.Street);
			System.debug('Reg Address: ' + guResponse.User_x.RegistrationAddress.CityName);
			System.debug('Reg Address: ' + guResponse.User_x.RegistrationAddress.StateOrProvince);
			System.debug('Reg Address: ' + guResponse.User_x.RegistrationAddress.Country);
			System.debug('Reg Address: ' + guResponse.User_x.RegistrationAddress.CountryName);
			System.debug('Reg Address: ' + guResponse.User_x.RegistrationAddress.Phone);
			System.debug('Reg Address: ' + guResponse.User_x.RegistrationAddress.PostalCode);
		}
	}
	
	public static void doAddItem() {
		ebay.setupSecurity('AddItem');

		AddItemRequestType.AddItemRequestType_e gur = new AddItemRequestType.AddItemRequestType_e();

		gur.setVersion('563'); 
		gur.setDetailLevel(new String[] {'ReturnAll'});
		gur.setItem(new ItemType.ItemType_e());
		gur.getItem().setCountry('US');
		gur.getItem().setCurrency_x('USD');
		gur.getItem().setDescription('This is a full description of the item.');
		gur.getItem().setListingDuration('Days_7');
		gur.getItem().setLocation('San Francisco, CA');
		gur.getItem().setPaymentMethods(new List<String>{'PayPal', 'VisaMC'});
		gur.getItem().setPayPalEmailAddress('djcarroll@gmail.com');
		gur.getItem().setPrimaryCategory(new CategoryType.CategoryType_e());
		gur.getItem().getPrimaryCategory().setCategoryId('279');
		gur.getItem().setQuantity(3);
		gur.getItem().setStartPrice('25.99');
		gur.getItem().setTitle('Dave\'s new Book');
		gur.getItem().setApplyShippingDiscount(false);
		gur.getItem().setAutoPay(false);
		gur.getItem().setMotorsGermanySearchable(false);
		gur.getItem().setBuyerProtection('ItemIneligible');
		gur.getItem().setBuyItNowPrice('50.00');
		gur.getItem().setCategoryMappingAllowed(true);
		 
		AddItemResponseType.AddItemResponseType_e guResponse = ebay.AddItem(gur);
	}
	
	public static void doItemList() {
		ebay.setupSecurity('GetMyeBaySelling');
		 
		GetMyeBaySellingRequestType.GetMyeBaySellingRequestType_e sellingRequest = new GetMyeBaySellingRequestType.GetMyeBaySellingRequestType_e();

		sellingRequest.setVersion('563');
        sellingRequest.setDetailLevel(new String[] {'ReturnAll'});     
		
		ItemListCustomizationType.ItemListCustomizationType_e alist = new ItemListCustomizationType.ItemListCustomizationType_e();
		alist.setListingType('Auction'); 
		sellingRequest.setActiveList(alist);
		
		GetMyeBaySellingResponseType.GetMyeBaySellingResponseType_e resp = ebay.GetMyeBaySelling(sellingRequest);
		System.debug('Get Items: ' + resp.Ack);
		for (ItemType.ItemType_e item : resp.ActiveList.ItemArray.Item) {
			System.debug(item.ItemID);
		}
	} 
	
	public static void doGetCategories() {
		ebay.setupSecurity('GetCategories');
		 
		GetCategoriesRequestType.GetCategoriesRequestType_e catReq = new GetCategoriesRequestType.GetCategoriesRequestType_e();

		catReq.setVersion('563');
        catReq.setDetailLevel(new String[] {'ReturnAll'});     
		catReq.setLevelLimit(1);
	
		GetCategoriesResponseType.GetCategoriesResponseType_e resp = ebay.GetCategories(catReq);
		System.debug('Get Categories: ' + resp.Ack);
		List<Category__c> cats = new List<Category__c>();
		for (CategoryType.CategoryType_e cat : resp.CategoryArray.Category) {
			Category__c c = new Category__c();
			c.BestOfferEnabled__c = cat.BestOfferEnabled;
			c.AutoPayEnabled__c = cat.AutoPayEnabled == null ? false : cat.AutoPayEnabled;
			c.CategoryID__c = cat.CategoryID; 
			c.CategoryLevel__c = cat.CategoryLevel;
			c.Name = cat.CategoryName;
			c.CategoryParentID__c = cat.CategoryParentID[0];
			cats.add(c);
		}
		upsert cats;
	} 

	public static void doGetCategoryChildren(String catID) {
		Category__c pcat = [Select Id, CategoryID__c, CategoryLevel__c From Category__c Where Id = :catID];
		
		ebay.setupSecurity('GetCategories');
		 
		GetCategoriesRequestType.GetCategoriesRequestType_e catReq = new GetCategoriesRequestType.GetCategoriesRequestType_e();

		catReq.setVersion('563');
        catReq.setDetailLevel(new String[] {'ReturnAll'});     
		catReq.setCategoryParent(new String[] {pcat.CategoryID__c});
		System.debug('one');
		Integer level = pcat.CategoryLevel__c.intValue();
		System.debug('two');
		level++;
		System.debug('three');
		catReq.setLevelLimit(level);
		System.debug('four');
	
		GetCategoriesResponseType.GetCategoriesResponseType_e resp = ebay.GetCategories(catReq);
		System.debug('Get Categories: ' + resp.Ack);
		List<Category__c> cats = new List<Category__c>();
		for (CategoryType.CategoryType_e cat : resp.CategoryArray.Category) {
			if (cat.CategoryID != cat.CategoryParentID[0]) {
				Category__c c = new Category__c();
				c.BestOfferEnabled__c = cat.BestOfferEnabled;
				c.AutoPayEnabled__c = cat.AutoPayEnabled == null ? false : cat.AutoPayEnabled;
				c.CategoryID__c = cat.CategoryID; 
				c.CategoryLevel__c = cat.CategoryLevel;
				c.Name = cat.CategoryName;
				c.CategoryParentID__c = cat.CategoryParentID[0];
				c.Category__c = pcat.Id;
				c.LeafCategory__c = cat.LeafCategory == null ? false : cat.LeafCategory;
				cats.add(c);
			}
		}
		upsert cats;
	} 
    public static void deleteCategories() {
    	List<Category__c> ids = [Select Id From Category__c];
    	delete ids;
    }
    
	public static void doSetNotifications() {
		ebay.setupSecurity('SetNotificationPreferences');
		
		SetNotificationPreferencesRequestType.SetNotificationPreferencesRequestType_e setNot = new SetNotificationPreferencesRequestType.SetNotificationPreferencesRequestType_e();
		
		ApplicationDeliveryPreferencesType.ApplicationDeliveryPreferencesType_e adp = new ApplicationDeliveryPreferencesType.ApplicationDeliveryPreferencesType_e();
		adp.setAlertEmail('mailto://bidreceivednotification@az8h26wev5ckyaintcg4g66o.in.salesforce.com');
		adp.setAlertEnable('Enable');
		adp.setApplicationEnable('Enable');
		adp.setApplicationURL('mailto://bidreceivednotification@az8h26wev5ckyaintcg4g66o.in.salesforce.com');
		adp.setNotificationPayloadType('eBLSchemaSOAP');
		
		NotificationEventPropertyType.NotificationEventPropertyType_e nep = new NotificationEventPropertyType.NotificationEventPropertyType_e();
		 
		NotificationEnableType.NotificationEnableType_e net = new NotificationEnableType.NotificationEnableType_e();
		net.setEventType('BidReceived');
		net.setEventEnable('Enable');
		
		NotificationEnableType.NotificationEnableType_e net2 = new NotificationEnableType.NotificationEnableType_e();
		net2.setEventType('BidReceived');
		net2.setEventEnable('Enable');

		NotificationEnableArrayType.NotificationEnableArrayType_e neat = new NotificationEnableArrayType.NotificationEnableArrayType_e();
		neat.setNotificationEnable(new NotificationEnableType.NotificationEnableType_e[] { net });
		
		setNot.setVersion('561');
		setNot.setApplicationDeliveryPreferences(adp);
		setNot.setUserDeliveryPreferenceArray(neat);
		
		SetNotificationPreferencesResponseType.SetNotificationPreferencesResponseType_e guResponse = ebay.SetNotificationPreferences(setNot);

		System.debug(guResponse);
		if (guResponse.Ack == 'Failure') {
			System.debug(guResponse.Errors[0].LongMessage);
		} else {
			System.debug('Hmm, success?');
		}
	}
}